﻿using System;
using System.IO;
using System.Reflection;
using System.Resources;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace Eneta.Portal.Common
{
    public class Resources
    {
        static Resources()
        {
            //var intlAsssembly = Assembly.Load("Microsoft.SharePoint.intl, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
            var intlAsssembly = Assembly.Load("Microsoft.SharePoint.intl, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
            IntlResources = new ResourceManager("Microsoft.SharePoint", intlAsssembly);
        }

        private static ResourceManager IntlResources { get; set; }

        public static string ModerationStatusApproved
        {
            get
            {
                return IntlResources.GetString("ModerationStatusApproved");
            }
        }

        public static string ModerationStatusPending
        {
            get
            {
                return IntlResources.GetString("ModerationStatusPending");
            }
        }

        public static void ProvisionResourcesToApplicationResources(SPWebApplication webApp, string filePrefix, bool removeProvisionedFiles)
        {
            if (webApp == null) throw new ArgumentNullException("webApp");
            //try
            //{
            Logger.LogInfo("Provision resources: zones count = " + webApp.IisSettings.Keys.Count);
            Logger.LogInfo("Provision resources: file prefix = " + filePrefix);
            
                foreach (var zone in webApp.IisSettings.Keys)
                {
                    var zoneSettings = webApp.IisSettings[zone];
                    var sourcePath = string.Format("{0}\\Resources\\", SPUtility.GetGenericSetupPath(""));
                    var destPath = Path.Combine(zoneSettings.Path.ToString(), "App_GlobalResources");
                    var filePaths = !removeProvisionedFiles
                                                ? Directory.GetFiles(sourcePath, filePrefix + "*.resx")
                                                : Directory.GetFiles(destPath, filePrefix + "*.resx");

                    Logger.LogInfo("Provision resources: source path = " + sourcePath);
                    Logger.LogInfo("Provision resources: dest path = " + destPath);
                    Logger.LogInfo("Provision resources: filePaths count = " + filePaths.Length);

                    foreach (var filePath in filePaths)
                    {
                        Logger.LogInfo("Provision resources: file path = " + filePath);
                        var fileName = Path.GetFileName(filePath);
                        if (!removeProvisionedFiles)
                            File.Copy(filePath, Path.Combine(destPath, fileName), true);
                        else
                            File.Delete(Path.Combine(destPath, fileName));
                    }
                }
            //}
            //catch
            //{ }
        }

    }
}